package com.sendpo.zjx.sendpo.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.domain.UserDomain;
import com.sendpo.zjx.sendpo.domain.Userbankcard;
import com.sendpo.zjx.sendpo.domain.UserbankcardDomain;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.table.User;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/8/24.
 */

public class TixianActivity extends AppCompatActivity {

    @BindView(R.id.sex_iv)
    ImageView sexIv;
    @BindView(R.id.bankcard_iv)
    ImageView bankcardIv;
    @BindView(R.id.bank_name)
    TextView bankName;
    @BindView(R.id.g)
    TextView g;
    @BindView(R.id.card_bt)
    RelativeLayout cardBt;
    @BindView(R.id.price_tv)
    TextView priceTv;
    @BindView(R.id.alltixian_bt)
    Button alltixianBt;
    private String money="0.00";
    private String bankid;
    @BindView(value = R.id.useablemoney_tv)
    TextView useablemoney_tv;
    @BindView(value = R.id.money_edit)
    EditText money_edit;
    public static  final int REQUEST_BANK=103;
    public static  final int SELECET=103;
    public Dialog loadingdialog = null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tixian);
        ButterKnife.bind(this);
        Tools.initStatus(this, R.color.black);
        initComponent();
    }

    private void initComponent() {
        loadingdialog = new Dialog(this);
        loadingdialog.setContentView(R.layout.dialog_loading);
        loadingdialog.setCanceledOnTouchOutside(false);
        loadingdialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        queryAll();
    }

    @Override
    protected void onStart() {
        super.onStart();
        webUserServer();
    }

    @OnClick(value = {R.id.backbt, R.id.tixian_bt, R.id.card_bt})
    public void onMyClick(View view) {
        switch (view.getId()) {
            case R.id.backbt:
                finish();
                break;
            case R.id.tixian_bt:
                if(TextUtils.isEmpty(bankName.getText().toString().trim())){
                    Toast.makeText(getApplicationContext(),"请绑定银行卡",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(money_edit.getText().toString())){
                    Toast.makeText(getApplicationContext(),"不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                Double originmoney=Double.parseDouble(money);
                Double   tixianmoney=Double.parseDouble(money_edit.getText().toString());
                if(originmoney==0){
                    Toast.makeText(getApplicationContext(),"没有那么多钱可提┭┮﹏┭┮",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(tixianmoney==0){
                    Toast.makeText(getApplicationContext(),"提现金额为0┭┮﹏┭┮",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(originmoney<tixianmoney){
                    Toast.makeText(getApplicationContext(),"没有那么多钱可提┭┮﹏┭┮",Toast.LENGTH_SHORT).show();
                    return;
                }


                DecimalFormat decialFormat=new DecimalFormat("0.00");
                tixian( decialFormat.format(tixianmoney),bankid);
                break;
            case R.id.card_bt:
                Intent bank=new Intent(this,BankManagerActivity.class);
                bank.putExtra("type",SELECET);
                startActivityForResult(bank,REQUEST_BANK);
                break;

        }
    }
    public void webUserServer(){
        final List<User> userList= Xutils.queryAll(getApplication(),User.class);
        if(userList==null||userList.size()==0){
            return;
        }
        Map<String,String> map=new HashMap<>();
        map.put("userId",userList.get(0).getUserId()+"");
        Xutils.POST(HttpUrl.queryuserbyid, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson=new Gson();
                UserDomain userDomain= gson.fromJson(result,UserDomain.class);
                switch (userDomain.getState()){
                    case HttpCode.SUCCESS:
                        User user= userDomain.getData();
                        user.setId(userList.get(0).getId());
                        Xutils.update(getApplication(),user);
                        locationUserServer();
                        break;
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    };
    public void locationUserServer(){
        final List<User> userList= Xutils.queryAll(getApplication(),User.class);
        if(userList==null||userList.size()==0){
            return;
        }
        User user=userList.get(0);
        System.out.println("*****************getUserMoney:"+user.getUserMoney());
        DecimalFormat decialFormat=new DecimalFormat("0.00");
        money=decialFormat.format(Double.parseDouble(user.getUserMoney()));
        useablemoney_tv.setText("可用余额"+money+"元");
        alltixianBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                money_edit.setText(money);
            }
        });
    }
    public void queryAll(){
        final List<User> userList= Xutils.queryAll(getApplication(),User.class);
        if(userList==null||userList.size()==0){
            return;
        }
        User user=userList.get(0);
        Map<String,String> map=new HashMap();
        map.put("userId",user.getUserId()+"");
        Xutils.POST(HttpUrl.queryuserbank, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson=new Gson();
                UserbankcardDomain userbankcardDomain=gson.fromJson(result, UserbankcardDomain.class);
                switch (userbankcardDomain.getState()){
                    case HttpCode.SUCCESS:
                        if(userbankcardDomain.getData()==null||userbankcardDomain.getData().size()==0){
                            return;
                        }
                        Userbankcard userbankcard=userbankcardDomain.getData().get(0);
                        bankName.setText(userbankcard.getUserbankcardBankname());
                        g.setText("尾号"+userbankcard.getUserbankcardBankcard().substring(userbankcard.getUserbankcardBankcard().length()-4,userbankcard.getUserbankcardBankcard().length()));
                        bankid=userbankcard.getUserbankcardId()+"";
                        break;
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(RESULT_OK!=resultCode){
            return;
        }
        switch (requestCode){
            case REQUEST_BANK:
                if(data!=null){
                    Userbankcard userbankcard= (Userbankcard) data.getSerializableExtra("data");
                    bankName.setText(userbankcard.getUserbankcardBankname());
                    g.setText("尾号"+userbankcard.getUserbankcardBankcard().substring(userbankcard.getUserbankcardBankcard().length()-4,userbankcard.getUserbankcardBankcard().length()));
                    bankid=userbankcard.getUserbankcardId()+"";
                }

                break;
        }
    }

    //提现
    public void tixian(String userwithdrawalMoney,String userbankcardId){
        loadingdialog.show();
        final List<User> userList= Xutils.queryAll(getApplication(),User.class);
        if(userList==null||userList.size()==0){
            return;
        }
        User user=userList.get(0);
        Map<String,String> map=new HashMap<>();
        map.put("userId",""+user.getUserId());
        map.put("userwithdrawalMoney",userwithdrawalMoney);
        map.put("userbankcardId",userbankcardId);
        map.put("userwithdrawalState",0+"");
        Xutils.POST(HttpUrl.withdrawaladd, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                money_edit.getText().clear();
                webUserServer();
                loadingdialog.dismiss();
                try {
                    JSONObject jb=new JSONObject(result);
                    Toast.makeText(getApplicationContext(),jb.getString("message"),Toast.LENGTH_SHORT).show();
                    switch (jb.getInt("state")){
                        case HttpCode.SUCCESS:
                            break;
                        case HttpCode.ERROR:
                            break;
                        case HttpCode.THROW:
                            break;

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                loadingdialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
